package com.java.controller;

import com.java.domain.ShopCart;
import com.java.domain.Users;
import com.java.service.OrderService;
import com.java.service.ShopCartService;
import com.java.service.impl.OrderServiceImpl;
import com.java.service.impl.ShopCartServiceImpl;
import com.java.utils.ShopCartUtils;
import org.apache.commons.beanutils.BeanUtils;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @version 2020.3.4
 * @User 悠恋
 * @Data 2022/11/25
 * @Time 16:36
 */
@WebServlet("/shopcart/*")
public class ShopCartServlet extends BaseServlet {

    private ShopCartService shopCartService = new ShopCartServiceImpl();
    private Users users = null;

    //查询当前用户的商品
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
        users = (Users) request.getSession().getAttribute("user");
        //System.out.println(users);

        String uname = users.getuName();
        List<ShopCart> shopCartList = shopCartService.findAll(uname);
        // 发送数据   添加
        request.getSession().setAttribute("shopCartList", shopCartList);
        response.sendRedirect("../shopcart.jsp");
    }

    //添加商品
    public void addOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {

        ShopCart shopCart = new ShopCart();
        // 接收前端传过来的数据
        BeanUtils.populate(shopCart, request.getParameterMap());
        // 补全购买的用户名称
        users = (Users) request.getSession().getAttribute("user");
        //System.out.println(users);
        String uname = users.getuName();
        shopCart.setUname(uname);

        //System.out.println(uname);
        //System.out.println(shopCart.getUname());
        List<ShopCart> cartList = shopCartService.addOrUpdate(shopCart);
        // 将当前登录的用户的所有信息添加到集合中
        List<ShopCart> shopCartList = ShopCartUtils.getShopCartList(cartList, uname);
        // 判断方法
        if (shopCart.getGprice() != 0) {
            // 发送数据   添加
            request.getSession().setAttribute("shopCartList", shopCartList);
            response.sendRedirect("../shopcart.jsp");
        } else {
            // 调用业务 更新
            request.getSession().setAttribute("shopCartList", shopCartList);
        }
    }

    // 删除单个商品
    public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 接收传过来的id
        int gid = Integer.parseInt(request.getParameter("gid"));
        // 取出当前登录用户的所有信息
        users = (Users) request.getSession().getAttribute("user");
        String uname = users.getuName();
        // 调用删除方法
        List<ShopCart> cartList = shopCartService.delById(gid, uname);
        // 将当前登录的用户的所有信息添加到集合中
        List<ShopCart> shopCartList = ShopCartUtils.getShopCartList(cartList, uname);
        // 转发到页面
        request.getSession().setAttribute("shopCartList", shopCartList);
    }

    // 删除所有商品
    public void delAll(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String checkeds = request.getParameter("checkeds");

        String[] split = checkeds.split(",");
        // 取出当前登录用户的所有信息
        users = (Users) request.getSession().getAttribute("user");
        String uname = users.getuName();
        // 调用业务
        List<ShopCart> shopCartList = shopCartService.delAll(split, uname);
        // 转发到页面
        request.getSession().setAttribute("shopCartList", shopCartList);

        PrintWriter out = response.getWriter();
        out.println(1);


    }

}
